package mobilesmil.activity;

import mobilesmil.activity.composer.ComposerActivity;
import mobilesmil.services.MessageReceiver;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.PhoneLookup;
import android.view.View;
import android.widget.Button;

public class HomeActivity extends Activity {
	
	public Intent mPlayerIntent;	//used to negotiate between various activities
	public Intent mComposerIntent;	//used to negotiate between various activities
	public Intent mManagerIntent;	//used to negotiate between various activities
	private String mComposerTask;
	private String mComposerCreateNew;
	private String mManagerTask;
	private Thread mReceiveThread;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.home);
        String toNumber = null;
	//	TelephonyManager telephonyMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
	//	toNumber = telephonyMgr.getLine1Number();
        toNumber = this.getString(R.string.emulator_phone_number);
		if(toNumber != null && mReceiveThread == null)
		{
			MessageReceiver receiver = new MessageReceiver(toNumber, this);
			mReceiveThread = new Thread(receiver);
			mReceiveThread.start();
		}
        mComposerTask = this.getString(R.string.composerTask);
        mComposerCreateNew = this.getString(R.string.createNew);
        mManagerTask = this.getString(R.string.managerTask);
        
        ((Button)findViewById(R.id.btnBrowseSmils)).setOnClickListener(new View.OnClickListener() {
        	@Override
        	public void onClick(View view) {
        		mManagerIntent = new Intent(getApplicationContext(), FileBrowserActivity.class);
        		mManagerIntent.putExtra(mManagerTask, "");
        		startActivity(mManagerIntent);
        	}
        });
        
        ((Button)findViewById(R.id.btnCreateNewSmil)).setOnClickListener(new View.OnClickListener() {
        	@Override
        	public void onClick(View view) {
        		mComposerIntent = new Intent(getApplicationContext(), ComposerActivity.class);
        		mComposerIntent.putExtra(mComposerTask, mComposerCreateNew);
        		startActivity(mComposerIntent);
        	}
        });
   }
    
    /**
     * The notification is the icon and associated expanded entry in the
     * status bar.
     */
    public void showNotification(String fromNumber, String messageTitle) {
        // look up the notification manager service
        NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

        // The PendingIntent to launch our activity if the user selects this notification
        Intent manager = new Intent(getApplicationContext(), FileBrowserActivity.class);
		manager.putExtra(getString(R.string.managerTask), "");
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,manager, 0);

        // The ticker text, this uses a formatted string so our message could be localized
        String tickerText = "You have a new SMIL :)";

        // construct the Notification object.
        Notification notif = new Notification(R.drawable.icon, tickerText,
                System.currentTimeMillis());

        Uri contactUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(fromNumber));

        Cursor cur = this.getContentResolver().query(contactUri, new String[]{PhoneLookup.DISPLAY_NAME}, null, null, null);
        String fromContact = null;
        if(cur.moveToFirst())
            fromContact = cur.getString(0);
        if(fromContact != null)
            fromNumber = fromContact;
        
        // Set the info for the views that show in the notification panel.
        notif.setLatestEventInfo(this, "New SMIL from " + fromNumber, messageTitle + " - Check it out!", contentIntent);

        // after a 100ms delay, vibrate for 250ms, pause for 100 ms and
        // then vibrate for 500ms.
        notif.vibrate = new long[] { 100, 250, 100, 500};

        // Note that we use R.layout.incoming_message_panel as the ID for
        // the notification.  It could be any integer you want, but we use
        // the convention of using a resource id for a string related to
        // the notification.  It will always be a unique number within your
        // application.
        nm.notify(9, notif);
    }
}